package com.kaizige.realtor.Controller;


import com.kaizige.realtor.Dao.houseMsgDao;
import com.kaizige.realtor.Dao.houseOwnerDao;
import com.kaizige.realtor.Dao.managerDao;
import com.kaizige.realtor.Dao.tenantDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class loginController {
	@Autowired
	houseOwnerDao hod;
	@Autowired
	tenantDao td;
	@Autowired
	managerDao md;
	@Autowired
	houseMsgDao hmd;
	@RequestMapping("/login")
	public String login(@RequestParam("name") String name, @RequestParam("password") String password,
						@RequestParam("identity") String identity, HttpSession session, Model model){
		session.removeAttribute("error");
		Map<String, Object> stringObjectMap;
		if(identity.equals("manager")){
			stringObjectMap = md.queryByName(name);
		}
		else if(identity.equals("tenant")){
			stringObjectMap = td.queryByName(name);
		}
		else{
			stringObjectMap = hod.queryByName(name);
		}
		if(stringObjectMap == null){
			model.addAttribute("msg", "用户名或密码错误！");
			return "login";
		}
		String pwd = (String)stringObjectMap.get("password");
		if(!pwd.equals(password)){
			model.addAttribute("msg", "用户名或密码错误！");
			return "login";
		}
		session.setAttribute("LoginUser", name);
		session.setAttribute("identity", identity);
		String[] strs = {"平层", "跃层", "错层", "复式"};
		session.setAttribute("houseType",strs);
		if(identity.equals("manager")){
			return "redirect:/manIndex";
		}
		else if(identity.equals("tenant")){
			return "redirect:/tenIndex";
		}
		else{
			return "redirect:/ownIndex";
		}
	}
}
